package controller;

import gui.GUIManager;
import gui.Shape;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;

import model.OOModel;
import model.OOPackage;

public class ControllerManager {

	public Scanner scanner;
	public ModelGenerator modelgen;
	public PackageDiagramManager packdmanager;
	public ClassDiagramManager classdgrm; // Temp ClassDiagramManager
	
	private DirectoryTraverseTree dtTree;
	private ArrayList<TokensClass> arrtoken;
	ArrayList<OOPackage> ooModelpack;
	@SuppressWarnings("unused")
	private OOModel ooModel;
	
	/*
	 * for any control class to deal with GUI windows, we must
	 * have an object of GUIManager
	 */
	private GUIManager gui; 
	

	private static ControllerManager controllermanager;

	private ControllerManager() {
		scanner = new Scanner();
	}

	public static ControllerManager getDefault() {
		if (controllermanager == null) {
			controllermanager = new ControllerManager();
		}
		return controllermanager;
	}
	
	// generates the whole model, classes & packages are involved
	public void generateModel()
	{
		//TODO: implement
	}
	
	// to generate a package diagram
	public void generatePackageDiagram(String source){
		dtTree.processDirectory(new File(source));
		packdmanager.generateShapes();		
	}
	
	// to generate a class diagram
	public void generateClassDiagram(String source){
		try {
			arrtoken= scanner.scan(new FileReader(new File(source)));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		modelgen.generateModel(arrtoken);
		classdgrm.generateShapes();		
	}
	
	// called by the ProgramManager main() when introducing GUI and CTRL to each other :)
	public void setGUI(GUIManager gui)
	{
		this.gui = gui;
	}
	
	// called by an controller wishing to deal with GUI windows
	public GUIManager getGUI()
	{
		return gui;
	}
	
	// called by the ProgramManager main() when introducing OOModel to CTRL
	public void setModel(OOModel ooModel)
	{
		this.ooModel = ooModel;
		modelgen = new ModelGenerator(ooModel);
		packdmanager = new PackageDiagramManager(ooModel);
		classdgrm = new ClassDiagramManager(ooModel);
		dtTree = new DirectoryTraverseTree(ooModel);
	}

	public void drawShapes(ArrayList<Shape> shapes) {
		gui.drawShapes(shapes);
	}

}
